<?php

namespace Modules\Code\Entities;

use Modules\Core\Entities\BaseEntity as Model;

class CodeRecord extends Model
{
    protected $guarded = [];
    protected $table = 'code_records';

    static public $resourceName = '兑换记录';

    public function scopeSearch($query, $key)
    {
    	return $query->whereHas('code', function ($q) use ($key) {
    		return $q->where('code', $key)->orWhere('title', $key);
    	})->orWhereHas('user', function ($q) use ($key) {
    		return $q->where('phone', $key)->orWhere('name', $key);
    	});
    }

    public function user()
    {
    	return $this->belongsTo('\Modules\User\Entities\User', 'user_id');
    }

    public function code()
    {
    	return $this->belongsTo('\Modules\Code\Entities\Code', 'code_id');
    }
}
